/*
 * @Author: your name
 * @Date: 2021-10-27 09:55:16
 * @LastEditTime: 2021-11-03 16:29:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egg_ payment/app/controller/home.js
 */
'use strict';

const Controller = require('egg').Controller;
// 引入 nodemailer
const nodemailer = require('nodemailer');

// 创建一个SMTP客户端配置
const config = {
    host: 'smtp.163.com', 
    port: 25,
    auth: {
        user: 'm18811101669@163.com', //刚才注册的邮箱账号
        pass: 'MCXGWCKCEFDJODLD'  //邮箱的授权码，不是注册时的密码
    }
};

// 创建一个SMTP客户端对象
const transporter = nodemailer.createTransport(config);

class EmailController extends Controller {
  async index() {
    const { ctx } = this;

    // 提供推送的功能
    const email = (mail) => {
        transporter.sendMail(mail, function(error, info){
            if(error) {
                return console.log(error);
            }
            console.log('mail sent:', info.response);
        });
    }

    // 创建一个邮件对象
    const mail = {
        // 发件人
        from: '咒大西 <m18811101669@163.com>',
        // 主题
        subject: '八维创作平台',
        // 收件人
        to: '1269123544@qq.com',
        // 邮件内容，HTML格式
        text: '王八蛋我终于发送了' //接收激活请求的链接
    };

    try {
        await email(mail);
        ctx.body = {
            code: 200,
            msg: '推送成功'
        };
    } catch (e) {
        ctx.body = e;
    }

  }
}

module.exports = EmailController;
